package com.hsy.order.setting.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.*
import com.zj.btsdk.BluetoothService
import android.widget.TextView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ArrayAdapter
import com.hsy.order.R
import com.hsy.order.base.ui.activity.BaseActivity
import com.hsy.order.setting.store.PrinterStore

/**
 * Created on 2018/5/2.
 *
 * @author Yang
 */
class PrintersActivity : BaseActivity<PrinterStore>() {

    /**
     * 数据缓存器
     */
    override var store = PrinterStore(dispatcher)

    // Member fields
    private var mService: BluetoothService? = null
    private var mPairedDevicesArrayAdapter: ArrayAdapter<String>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.device_list)   //显示列表界面
        title = "设置蓝牙打印机"

        // Set result CANCELED incase the user backs out
        setResult(Activity.RESULT_CANCELED)

        // Initialize array adapters. One for already paired devices and
        // one for newly discovered devices
        mPairedDevicesArrayAdapter = ArrayAdapter(this, R.layout.device_name)

        // Find and set up the ListView for paired devices
        val pairedListView = findViewById<View>(R.id.paired_devices) as ListView
        pairedListView.adapter = mPairedDevicesArrayAdapter
        pairedListView.onItemClickListener = mDeviceClickListener

        mService = BluetoothService(this, null)

        // Get a set of currently paired devices
        val pairedDevices = mService!!.pairedDev

        // If there are paired devices, add each one to the ArrayAdapter
        if (pairedDevices.size > 0) {
            findViewById<View>(R.id.title_paired_devices).visibility = View.VISIBLE
            for (device in pairedDevices) {
                mPairedDevicesArrayAdapter!!.add(device.name + "\n" + device.address)
            }
        } else {
            val noDevices = "没有已匹配的设备"
            mPairedDevicesArrayAdapter!!.add(noDevices)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mService != null) {
            mService!!.cancelDiscovery()
        }
        mService = null
    }

    // The on-click listener for all devices in the ListViews
    private val mDeviceClickListener = OnItemClickListener { _, v, _, _ ->
        //点击列表项，连接设备
// Cancel discovery because it's costly and we're about to connect
        mService!!.cancelDiscovery()

        // Get the device MAC address, which is the last 17 chars in the View
        val info = (v as TextView).text.toString()
        val address = info.substring(info.length - 17)

        // Create the result Intent and include the MAC address
        val intent = Intent()
        intent.putExtra(EXTRA_DEVICE_ADDRESS, address)
        Log.d("连接地址", address)

        // Set result and finish this Activity
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    companion object {
        const val EXTRA_DEVICE_ADDRESS = "device_address"
    }
}